package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.service.ReportService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class ReportServiceImpl implements ReportService {
         @Autowired
         private MemberDao memberDao;



    @Override
    public Map<String ,Object[]> findReportNumber() {
        Map<String ,Object[]> num = new HashMap<>();

        List<String> months = new ArrayList<>();
        //创建日历类
        Calendar cal = Calendar.getInstance();
        //获得往前推-36个月
        cal.add(Calendar.MONTH,-36);
        //格式创建成"yyyy.MMMM"
        DateFormat df = new SimpleDateFormat("yyyy.MM");
        for (int i = 0; i <12 ; i++) {
            //往日历集合添加一个月
               cal.add(Calendar.MONTH,1);
               //将日历的月份Date类型转换成String类型存到集合里

               months.add(df.format(cal.getTime()));

        }
        List<Integer> memeberCounts = new ArrayList<>();
        for (String month : months) {


            String endDate = month+".31";
          Long count =    memberDao.countByRegTime(endDate);
          memeberCounts.add(count.intValue());
        }
        Object [] month = months.toArray();
        Object [] memeberCount = memeberCounts.toArray();
        num.put("months",month);
        num.put("memberCount",memeberCount);


        return num;
    }

}
